package com.nuanshui.heatedloan.credit.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.nuanshui.framework.consts.TransactionManagerType;
import com.nuanshui.framework.lang.SysStringUtils;
import com.nuanshui.framework.res.qiniu.Qiniu;
import com.nuanshui.framework.security.DESC;
import com.nuanshui.heatedloan.baseservice.facade.Image2MediaImageConvert;
import com.nuanshui.heatedloan.baseservice.facade.ImageService;
import com.nuanshui.heatedloan.baseservice.service.CommonServiceImpl;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.constant.ServiceConstant;
import com.nuanshui.heatedloan.credit.dal.mapper.CustomerIdCardInfoMapper;
import com.nuanshui.heatedloan.credit.facade.CustomerIdCardInfoService;
import com.nuanshui.heatedloan.credit.form.CustomerIdCardInfoForm;
import com.nuanshui.heatedloan.credit.utils.IdCardUtil;
import com.nuanshui.heatedloan.credit.vo.CustomerIdCardInfoVO;
import com.nuanshui.heatedloan.enums.FileBelong;
import com.nuanshui.heatedloan.model.Image;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(ServiceConstant.CUSTOMERIDCARDINFOSERVICE)
@Transactional(readOnly=true,value= TransactionManagerType.CREDIT_TRANSACTION_MANAGER)
public class CustomerIdCardInfoServiceImpl  extends CommonServiceImpl implements CustomerIdCardInfoService{
	
    @Reference(url = GlobalParam.BASESERVICE_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.BASESERVICE_DUBBO_SERVER_VERSION)
	private ImageService imageService;
    
    @Reference(url = GlobalParam.BASESERVICE_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.BASESERVICE_DUBBO_SERVER_VERSION)
    private Image2MediaImageConvert image2MediaImageConvert;
     
	@Autowired
	private CustomerIdCardInfoMapper customerIdCardInfoMapper;

	@Autowired
	private Qiniu qiniuNonProd;

	
	@Override
	@Transactional(readOnly=false,value= TransactionManagerType.CREDIT_TRANSACTION_MANAGER)
	public CustomerIdCardInfoForm saveCustomerIdCardInfoVO(CustomerIdCardInfoForm customerIdCardInfoForm) {
	 //首先保持图片的信息
	 //正面，反面，手持照片
		this.setImageId(customerIdCardInfoForm);
		customerIdCardInfoForm.setSex(this.getSexByIdCard(customerIdCardInfoForm.getIdCard()));
		customerIdCardInfoForm.setAge(this.getAgeByIdCard(customerIdCardInfoForm.getIdCard()));
	    customerIdCardInfoMapper.saveCustomerIdCardInfo(customerIdCardInfoForm);
		return customerIdCardInfoForm;
	}
	
	
	private Image insertIdCardImage(String imageMsg){
		 //MediaForm faceIdCardMedia = JSON.parseObject(imageMsg,MediaForm.class);
		 //image2MediaImageConvert.mediaFromImage2Image(faceIdCardMedia);
		 Image  image =JSON.parseObject(imageMsg,Image.class);
		 String bucketName = qiniuNonProd.genBucketName(FileBelong.PRODUCT);
		 image.setKey(image.getKey() == null ? null : qiniuNonProd.qiniuKeyToLocalKey(image.getKey(), bucketName));
		 image.setBelong(FileBelong.PRODUCT);
		 String  imageNew=imageService.insert(image);
		 image.setId(imageNew);
		 return image;
	}

	private void setImageId(CustomerIdCardInfoForm customerIdCardInfoForm){
		String faceIdCardImageStr=customerIdCardInfoForm.getFaceIdCardImageStr();
		if(!StringUtils.isEmpty(faceIdCardImageStr)){
			customerIdCardInfoForm.setFaceIdCardImage(this.insertIdCardImage(customerIdCardInfoForm.getFaceIdCardImageStr()));
		}
		String rearIdCardImageStr=customerIdCardInfoForm.getRearIdCardImageStr();
		if(!StringUtils.isEmpty(rearIdCardImageStr)){
			customerIdCardInfoForm.setRearIdCardImage(this.insertIdCardImage(customerIdCardInfoForm.getRearIdCardImageStr()));
		}
		String handIdCardImageStr=customerIdCardInfoForm.getHandIdCardImageStr();
		if(!StringUtils.isEmpty(handIdCardImageStr)){
			customerIdCardInfoForm.setHandIdCardImage((this.insertIdCardImage(customerIdCardInfoForm.getHandIdCardImageStr())));
		}
	}

	@Override
	@Transactional(readOnly=false,value= TransactionManagerType.CREDIT_TRANSACTION_MANAGER)
	public CustomerIdCardInfoForm updateCustomerIdCardInfoVO(CustomerIdCardInfoForm customerIdCardInfoForm) {
		//找出之前的信息，把其图片信息变为无效
		CustomerIdCardInfoVO customerIdCardInfoVO=this.loadVO(customerIdCardInfoForm.getId());
		if(customerIdCardInfoVO.getFaceIdCardImage()!=null){
			imageService.delete(customerIdCardInfoVO.getFaceIdCardImage().getId());
		}
		if(customerIdCardInfoVO.getRearIdCardImage()!=null){
			imageService.delete(customerIdCardInfoVO.getRearIdCardImage().getId());
		}
		if(customerIdCardInfoVO.getHandIdCardImage()!=null){
			imageService.delete(customerIdCardInfoVO.getHandIdCardImage().getId());
		}
		//插入新的图片
		this.setImageId(customerIdCardInfoForm);
		customerIdCardInfoForm.setSex(this.getSexByIdCard(customerIdCardInfoForm.getIdCard()));
		customerIdCardInfoForm.setAge(this.getAgeByIdCard(customerIdCardInfoForm.getIdCard()));
		customerIdCardInfoMapper.updateCustomerIdCardInfo(customerIdCardInfoForm);
		return customerIdCardInfoForm;
	}

	@Override
	@Transactional(readOnly=false,value= TransactionManagerType.CREDIT_TRANSACTION_MANAGER)
	public void deleteCustomerIdCardInfoVO(String id) {
		customerIdCardInfoMapper.deleteCustomerIdCardInfo(id);
	}

	@Override
	public CustomerIdCardInfoVO loadVO(String id) {
		CustomerIdCardInfoVO customerIdCardInfoVO=new CustomerIdCardInfoVO();
		customerIdCardInfoVO=customerIdCardInfoMapper.loadVO(id);
		this.getImageId(customerIdCardInfoVO);
		return customerIdCardInfoVO;
	}

	@Override
	public CustomerIdCardInfoVO loadByUserId(String userId){
		CustomerIdCardInfoVO customerIdCardInfoVO=customerIdCardInfoMapper.loadByUserId(userId);
		this.getImageId(customerIdCardInfoVO);
		return customerIdCardInfoVO;
	}

	private void getImageId(CustomerIdCardInfoVO customerIdCardInfoVO){
		if(customerIdCardInfoVO!=null){
			//已经是密文无需加密
			//customerIdCardInfoVO.setIdCard(DESC.encrypt3DES(customerIdCardInfoVO.getIdCard()));
			if(customerIdCardInfoVO.getFaceIdCardImage()!=null){
				customerIdCardInfoVO.setFaceIdCardImage(imageService.load(customerIdCardInfoVO.getFaceIdCardImage().getId()));
			}
			if(customerIdCardInfoVO.getRearIdCardImage()!=null){
				customerIdCardInfoVO.setRearIdCardImage(imageService.load(customerIdCardInfoVO.getRearIdCardImage().getId()));
			}
			if(customerIdCardInfoVO.getHandIdCardImage()!=null){
				customerIdCardInfoVO.setHandIdCardImage(imageService.load(customerIdCardInfoVO.getHandIdCardImage().getId()));
			}
		}
	}

    public List<CustomerIdCardInfoVO> getAllCustomerIdCardInfoVO(){
		List<CustomerIdCardInfoVO> customerIdCardInfoVOs=new ArrayList<CustomerIdCardInfoVO>();
		customerIdCardInfoVOs=customerIdCardInfoMapper.getAllCustomerIdCardInfoVO();
		return customerIdCardInfoVOs;
	}

	/***
	 * 获取年龄
	 * @param mixIdCard
	 * @return
	 */
	private int getAgeByIdCard(String mixIdCard){
		if(SysStringUtils.isEmpty(mixIdCard)){
			return 0;
		}
		String idCard = DESC.decrypt3DES(mixIdCard);
		return IdCardUtil.getAgeByIdCard(idCard);
	}

	/***
	 * 性别  1 男 2女  3  未知
	 * @param mixIdCard
	 * @return
	 */
   private int getSexByIdCard(String mixIdCard){
		if(SysStringUtils.isEmpty(mixIdCard)){
			return 3;
		}
	   String idCard = DESC.decrypt3DES(mixIdCard);
	   return  Integer.parseInt(IdCardUtil.getGenderByIdCard(idCard));
   }
}
